package ro.ubbcluj.android.PhysiTrack;
import graphview.GraphView;
import graphview.GraphView.GraphViewData;
import graphview.GraphView.GraphViewSeries;
import graphview.LineGraphView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class DiagramActivity extends Activity{

	private static final String TAG = DiagramActivity.class.getSimpleName(); 
	private  double[][] result = new double[100][5];
	private int db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.diagramm);

		String path =  getIntent().getExtras().getString("filepath");
		Log.d(TAG, path);
		resultFromFile(path);
		

		final LinearLayout layout = (LinearLayout) findViewById(R.id.grafikon); 

		GraphViewData[] dataMotion = new GraphViewData[db];  
		 
		for (int i=0; i<db; i++) {  
			 
			dataMotion[i] = new GraphViewData(result[i][1],result[i][2]);  
		}  
		GraphViewSeries DiagrMotion = new GraphViewSeries(dataMotion);  

		final GraphView graphView = new LineGraphView(this, "");  
		graphView.addSeries(DiagrMotion); 
		graphView.setViewPort(0, layout.getWidth());
		graphView.setScrollable(true); 
		graphView.setScalable(true);  

 
		GraphViewData[] dataVel = new GraphViewData[db];  
		
		for (int i=0; i<db; i++) {  
			
			dataVel[i] = new GraphViewData(result[i][0],result[i][3]);  
		}  
		GraphViewSeries DiagrVel = new GraphViewSeries(dataVel);  

		final GraphView graphView2 = new LineGraphView(this, "");  
		graphView2.addSeries(DiagrVel);  
		graphView2.setViewPort(0, layout.getWidth());
		graphView2.setScrollable(true);
		graphView2.setScalable(true);  
		
		GraphViewData[] dataAcc = new GraphViewData[db];  
		 
		for (int i=0; i<db; i++) {  
			
			dataAcc[i] = new GraphViewData(result[i][0],result[i][4]);  
		}  
		GraphViewSeries DiagrAcc = new GraphViewSeries(dataAcc);  

		final GraphView graphView3 = new LineGraphView(this, "");  
		graphView3.addSeries(DiagrAcc);
		graphView3.setViewPort(0, layout.getWidth());
		graphView3.setScrollable(true);  
		graphView3.setScalable(true);  


		Button motion= (Button)findViewById(R.id.motion);
		motion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView);
			}
		});

		Button acceleration= (Button)findViewById(R.id.acceleration);
		acceleration.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView3);
			}
		});
		Button velocity= (Button)findViewById(R.id.velocity);
		velocity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView2);
			}
		});


		Button back = (Button)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DiagramActivity.this.finish();
			}
		});
	}
	
	public void resultFromFile(String fileName) {
		
		try{
			Log.d(TAG, "start read from file");
			//File file = Environment.getExternalStorageDirectory();
			File txt = new File(fileName);
			FileReader fr = new FileReader(txt);
	        BufferedReader in = new BufferedReader(fr);
	       
	          
	        String line = "";
	        int i=0;
	        
	        while ((line = in.readLine()) != null) {
				String[] sor= line.split(" ");
					for(int j=0;j<5;j++){
						result[i][j]=Double.parseDouble(sor[j]);
						Log.d(TAG, result[i][j]+"");
					}
				i++;
	        }
	        
	        db=i;
	        //System.out.print("DARABDIAGRAM"+db);
	        in.close();
			Log.d(TAG, "stop read from file");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void kiir(double[][] result2){
		for(int i=0;i<100;i++)
		{	
			for(int j=0;j<5;j++)
		
			{
				System.out.print(result2[i][j]+" ");
			}
			System.out.println();
		}	
	}
	@Override
	public void onBackPressed() {}
}